/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * POLineFrame.java
 *
 * Created on Jun 23, 2011, 3:22:09 PM
 */

package gbbmgmt;

import org.jdesktop.application.Action;
import java.util.*;
import javax.swing.JOptionPane;

/**
 *
 * @author dchapman
 */
public class POLineFrame extends javax.swing.JFrame {

    public int POIndex = 0;

    public ArrayList<String[]> Wearhouse;
    public ArrayList<String[]> Desc;

    /** Creates new form POLineFrame */
    public POLineFrame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jComboBoxWearhouse = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jComboBoxDesc = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldStockCode = new javax.swing.JTextField();
        jTextFieldQty = new javax.swing.JTextField();
        jButtonAdd = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(gbbmgmt.GBBMGMTApp.class).getContext().getResourceMap(POLineFrame.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jComboBoxWearhouse.setName("jComboBoxWearhouse"); // NOI18N
        jComboBoxWearhouse.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
                jComboBoxWearhousePopupMenuWillBecomeInvisible(evt);
            }
            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
            }
        });

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jComboBoxDesc.setName("jComboBoxDesc"); // NOI18N
        jComboBoxDesc.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
                jComboBoxDescPopupMenuWillBecomeInvisible(evt);
            }
            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
            }
        });

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jTextFieldStockCode.setText(resourceMap.getString("jTextFieldStockCode.text")); // NOI18N
        jTextFieldStockCode.setName("jTextFieldStockCode"); // NOI18N

        jTextFieldQty.setName("jTextFieldQty"); // NOI18N
        jTextFieldQty.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextFieldQtyKeyPressed(evt);
            }
        });

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(gbbmgmt.GBBMGMTApp.class).getContext().getActionMap(POLineFrame.class, this);
        jButtonAdd.setAction(actionMap.get("AddLine")); // NOI18N
        jButtonAdd.setText(resourceMap.getString("jButtonAdd.text")); // NOI18N
        jButtonAdd.setName("jButtonAdd"); // NOI18N

        jButton2.setAction(actionMap.get("cancel")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        jButton3.setAction(actionMap.get("SearchStockCode")); // NOI18N
        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jComboBoxDesc, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jComboBoxWearhouse, 0, 260, Short.MAX_VALUE)
                                .addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jTextFieldStockCode, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton3))
                            .addComponent(jTextFieldQty, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jButtonAdd))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jComboBoxWearhouse, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jComboBoxDesc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextFieldStockCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextFieldQty, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonAdd)
                    .addComponent(jButton2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBoxWearhousePopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_jComboBoxWearhousePopupMenuWillBecomeInvisible
        // TODO add your handling code here:
        String Itm = jComboBoxWearhouse.getSelectedItem().toString();

        for(String s[] : Wearhouse){
            String cmp = s[0] + " - " + s[1];
            if (cmp.equals(Itm)) {
                DBA.openDBCon();
                DBA.GetWearhouseData(s[0]);
                Desc = DBA.RunSQLQuery("SELECT StockID,TableID,WHID FROM StockCode WHERE WHID = '" +
                        Integer.toString(DBA.WearhouseID) + "'");
                jComboBoxDesc.removeAllItems();
                jComboBoxDesc.addItem("");

                for(String d[] : Desc){
                    if(d[2].equals("100")){
                        DBA.GetMaltData(d[1]);
                        jComboBoxDesc.addItem(DBA.MaltName);
                    } else if (d[2].equals("110")){
                        DBA.GetHopsData(d[1]);
                        jComboBoxDesc.addItem(DBA.HopName);
                    } else if (d[2].equals("120")){
                        DBA.GetYeastData(d[1]);
                        jComboBoxDesc.addItem(DBA.YeastDesc);
                    } else {
                        DBA.GetSuppliesData(d[1]);
                        jComboBoxDesc.addItem(DBA.SupName);
                    }
                }

                jComboBoxDesc.setEnabled(true);

                DBA.closeDBCon();
            }
        }
    }//GEN-LAST:event_jComboBoxWearhousePopupMenuWillBecomeInvisible

    private void jComboBoxDescPopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_jComboBoxDescPopupMenuWillBecomeInvisible
        // TODO add your handling code here:
        String WID = "0";
        String Itm = jComboBoxWearhouse.getSelectedItem().toString();
        for(String s[] : Wearhouse){
            String cmp = s[0] + " - " + s[1];
            if (cmp.equals(Itm)) {
                DBA.openDBCon();
                DBA.GetWearhouseData(s[0]);
                WID = Integer.toString(DBA.WearhouseID);
                DBA.closeDBCon();
            }
        }

        Itm = jComboBoxDesc.getSelectedItem().toString();
        DBA.openDBCon();

        for(String d[] : Desc){
            String cmp = "";
            if(WID.equals("100")){
                DBA.GetMaltData(d[1]);
                cmp = DBA.MaltName;
            } else if(WID.equals("110")){
                DBA.GetHopsData(d[1]);
                cmp = DBA.HopName;
            } else if(WID.equals("120")){
                DBA.GetYeastData(d[1]);
                cmp = DBA.YeastDesc;
            } else {
                DBA.GetSuppliesData(d[1]);
                cmp = DBA.SupName;
            }
            if(cmp.equals(Itm)){
                jTextFieldStockCode.setText(d[0]);
            }
        }
        DBA.closeDBCon();

        jTextFieldQty.setEnabled(true);
    }//GEN-LAST:event_jComboBoxDescPopupMenuWillBecomeInvisible

    private void jTextFieldQtyKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldQtyKeyPressed
        // TODO add your handling code here:
        jButtonAdd.setEnabled(true);
    }//GEN-LAST:event_jTextFieldQtyKeyPressed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new POLineFrame().setVisible(true);
            }
        });
    }

    @Action
    public void cancel() {
        dispose();
    }

    public void setIndex(int i){
        POIndex = i;
    }

    public void LoadList(){
        DBA.openDBCon();
        Wearhouse = DBA.RunSQLQuery("SELECT WearhouseID,WHDesc FROM Wearhouse");
        DBA.closeDBCon();

        for(String s[] : Wearhouse){
            jComboBoxWearhouse.addItem(s[0] + " - " + s[1]);
        }

        jComboBoxDesc.setEnabled(false);
        jTextFieldQty.setEnabled(false);
        jButtonAdd.setEnabled(false);
    }

    @Action
    public void SearchStockCode() {
        int code = Integer.parseInt(jTextFieldStockCode.getText().trim());

        if (code > 0){
            DBA.openDBCon();

            //Get Stock code.
            DBA.GetStockCodeData(Integer.toString(code));

            //Get Wearhouse Data.
            DBA.GetWearhouseData(Integer.toString(DBA.WHID));

            //Select Appropriate Wearhouse in combo box.
            String Itm = Integer.toString(DBA.WearhouseID);
            jComboBoxWearhouse.setSelectedItem(Itm + " - " + DBA.WHDesc);

            //Load Description box.
            Desc = DBA.RunSQLQuery("SELECT StockID,TableID,WHID FROM StockCode WHERE WHID = '" +
                        Integer.toString(DBA.WearhouseID) + "'");
            jComboBoxDesc.removeAllItems();
            jComboBoxDesc.addItem("");

            for(String d[] : Desc){
                if(d[2].equals("100")){
                    DBA.GetMaltData(d[1]);
                    jComboBoxDesc.addItem(DBA.MaltName);
                } else if (d[2].equals("110")){
                    DBA.GetHopsData(d[1]);
                    jComboBoxDesc.addItem(DBA.HopName);
                } else if (d[2].equals("120")){
                    DBA.GetYeastData(d[1]);
                    jComboBoxDesc.addItem(DBA.YeastDesc);
                } else {
                    DBA.GetSuppliesData(d[1]);
                    jComboBoxDesc.addItem(DBA.SupName);
                }
            }

            jComboBoxDesc.setEnabled(true);

            //Select Appropriate Item.
            if(Integer.toString(DBA.WHID).equals("100")){
                DBA.GetMaltData(Integer.toString(DBA.TableID));
                Itm = DBA.MaltName;
            } else if(Integer.toString(DBA.WHID).equals("110")){
                DBA.GetHopsData(Integer.toString(DBA.TableID));
                Itm = DBA.HopName;
            } else if(Integer.toString(DBA.WHID).equals("120")){
                DBA.GetYeastData(Integer.toString(DBA.TableID));
                Itm = DBA.YeastDesc;
            } else {
                DBA.GetSuppliesData(Integer.toString(DBA.TableID));
                Itm = DBA.SupName;
            }

            jComboBoxDesc.setSelectedItem(Itm);

            DBA.closeDBCon();

            jTextFieldQty.setEnabled(true);
        }
    }

    @Action
    public void AddLine() {
        //Add a line to the PO.
        int response = JOptionPane.showConfirmDialog(null,
                "Warning, this will add a line to PO, continue?",
                "Warning",JOptionPane.WARNING_MESSAGE);
        if (response == 0){
            DBA.openDBCon();

            String fields = "POLPN,POLQty,POLRcvd";
            String rcvd = "0";
            String query = "'" + jTextFieldStockCode.getText() + "','" +
                    jTextFieldQty.getText() + "','" +
                    rcvd + "'";
            DBA.RunSQLInsertQuery("POLines", fields, query);
            //String idx = DBA.GetLastIndex("POLines", "POLID");
            String idx = Integer.toString(Global.lastInsertID);

            //Now add this line item to the csv in PO number set above.

            DBA.GetPurchaseOrderData(Integer.toString(POIndex));

            String newCSV = DBA.POCSV;

            if(newCSV.length()<1){
                newCSV = idx;
            } else {
                newCSV = newCSV + "," + idx;
            }

            query = "UPDATE PurchaseOrder SET POCSV='" +
                        newCSV + "' WHERE POID = '" + Integer.toString(POIndex) + "'";
            DBA.RunSQLUpdateQuery(query);

            DBA.closeDBCon();

            Global.updateTable = true;

            dispose();
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButtonAdd;
    private javax.swing.JComboBox jComboBoxDesc;
    private javax.swing.JComboBox jComboBoxWearhouse;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField jTextFieldQty;
    private javax.swing.JTextField jTextFieldStockCode;
    // End of variables declaration//GEN-END:variables

}
